/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sims.execution;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import sims.entity.*;
import sims.io.StatisticsIO;

/**
 *
 * @author BinhMV
 */
public class Statistics {

    public int totalBills(String time) throws Exception {
        int count = 0;
        ArrayList<Order> lstOrders = new StatisticsIO().readOrderByTime(time);
        for (int i = 0; i < lstOrders.size(); i++) {
            if (lstOrders.get(i).getType().equals("sell")) {
                count++;
            }
        }
        return count;
    }

    public int totalBillsCancell(String time) throws Exception {
        int count = 0;
        ArrayList<Order> lstOrders = new StatisticsIO().readOrderByTime(time);
        for (int i = 0; i < lstOrders.size(); i++) {
            if (lstOrders.get(i).getStatus().equals("cancelled") && lstOrders.get(i).getType().equals("sell")) {
                count++;
            }
        }
        return count;
    }

    public float totalReceipts(String time) throws Exception {
        ArrayList<Order> lstOrders = new StatisticsIO().readOrderByTime(time);
        float total = 0;
        for (int i = 0; i < lstOrders.size(); i++) {
            if (lstOrders.get(i).getType().equals("sell") && lstOrders.get(i).getStatus().equals("successed")) {
                total = total + lstOrders.get(i).getTotal();
            }
        }
        return total;
    }

    public int totalItemsReturns(String time) throws Exception {
        return new StatisticsIO().totalItemsReturn(time);
    }

    public void checkBillsWaitting() throws Exception {
        StatisticsIO statisticsIO = new StatisticsIO();
        statisticsIO.cancellOrders(statisticsIO.waitingBills());
    }

    public DefaultTableModel setModelOrders() throws Exception {
        DefaultTableModel model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        String[] column = {"Order ID", "Date", "Type", "Total money", "Status"};
        model.setColumnIdentifiers(column);
        ArrayList<Order> lstOrders = new StatisticsIO().readOrder();

        for (int i = 0; i < lstOrders.size(); i++) {
            Object[] row = {lstOrders.get(i).getOrderID(), lstOrders.get(i).getDatetime(), lstOrders.get(i).getType(), lstOrders.get(i).getTotal(), lstOrders.get(i).getStatus()};
            model.addRow(row);
        }
        return model;
    }
    
    public User getUserByOrderId(int orderId) throws Exception{
        User user  = new StatisticsIO().getOrderInfo(orderId);
                return user;
    }
    
    public Order getOrderInfo(int orderId) throws Exception{
       ArrayList<Order> lstOrders = new StatisticsIO().readOrder();

        for (int i = 0; i < lstOrders.size(); i++) {
            if(lstOrders.get(i).getOrderID()== orderId){
                return lstOrders.get(i);
            }
        }
        return null;
    }

    public DefaultTableModel setModelItemInOrder(int orderId) throws Exception{
        DefaultTableModel model = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        String[] column = {"Product ID","Product Name","Price","Amount"};
        model.setColumnIdentifiers(column);
        ArrayList<OrderDetail> lstDetail = new StatisticsIO().getItemInOrder(orderId);
        for (int i = 0; i < lstDetail.size(); i++) {
            Object[] row = {lstDetail.get(i).getItemID(),lstDetail.get(i).getItemName(),lstDetail.get(i).getPriceSell(),lstDetail.get(i).getAmount()};
            model.addRow(row);
        }
        return model;      
    }
}
